
package com.xtsoft.platform.base.database.service.impl;

import com.xtsoft.core.common.YesNo;
import com.xtsoft.core.service.impl.GenericServiceImpl;
import com.xtsoft.platform.base.database.domain.DbConnect;
import com.xtsoft.platform.base.database.repository.DbConnectRepository;
import com.xtsoft.platform.base.database.service.DbConnectService;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;
/*
*    上海信拓信息技术有限公司 Koala企业综合信息管理系统
*    Copyright (C) 2010-2015 ShangHai XinTuo Software Inc.
*/
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("dbConnectService")
public class DbConnectServiceImpl extends GenericServiceImpl<DbConnect,String, DbConnectRepository> implements DbConnectService {

	@Autowired
	public DbConnectRepository dbConnectRepository;

	@Override
	public Connection connect(DbConnect dbc) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		String driverName = dbc.getDbDriver(); 
		
		String dbURL = dbc.getUrl(); 
				 
		String userName = dbc.getUsername();

		String userPwd = dbc.getPassword(); 
				
		Connection dbConn = null;
		
		Class.forName(driverName).newInstance();
		
		dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
		
		return dbConn;
	}



	/**
	 * 编写数据库连接测试方法
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	@Override
	@Transactional
	public boolean test(DbConnect dbc) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		Connection dbConn = connect(dbc);
		if(null != dbConn) {
			dbc.setState(YesNo.Yes.getIntValue());
			return true;
		}
		return false;
	}

}

